﻿using ImageMagick;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImageHandler.Framework
{
    public class ImageMagickHelper
    {
        /// <summary>
        /// GIF的每一帧已保存为单独的图片
        /// </summary>
        /// <param name="gifPath"></param>
        /// <returns></returns>
        public static List<string> SaveFrames(string gifPath)
        {
            List<string> frames = new List<string>();
            string folder = Path.GetDirectoryName(gifPath);
            string name = Path.GetFileNameWithoutExtension(gifPath);
            using (var collection = new MagickImageCollection(gifPath))
            {
                int frameIndex = 0;
                foreach (var image in collection)
                {
                    // 设置输出文件名
                    string outputFileName = Path.Combine(folder, $"{name}_{frameIndex}.png");
                    // 保存每一帧为单独的图片
                    image.Write(outputFileName);
                    frames.Add(outputFileName);
                    frameIndex++;
                }
            }
            return frames;
        }
    }
}
